package stub;

import base.ArrayOfFood;
import base.Food;
import javax.xml.rpc.JAXRPCException;
import javax.xml.namespace.QName;
import javax.microedition.xml.rpc.Operation;
import javax.microedition.xml.rpc.Type;
import javax.microedition.xml.rpc.ComplexType;
import javax.microedition.xml.rpc.Element;

public class FoodServicesSoap_Stub implements FoodServicesSoap, javax.xml.rpc.Stub {

    private String[] _propertyNames;
    private Object[] _propertyValues;

    public FoodServicesSoap_Stub() {
        _propertyNames = new String[]{ENDPOINT_ADDRESS_PROPERTY};
        _propertyValues = new Object[]{utils.Config.getServerAddress() + "FoodServices.asmx"};
    }

    public void _setProperty(String name, Object value) {
        int size = _propertyNames.length;
        for (int i = 0; i < size; ++i) {
            if (_propertyNames[i].equals(name)) {
                _propertyValues[i] = value;
                return;
            }
        }
        // Need to expand our array for a new property
        String[] newPropNames = new String[size + 1];
        System.arraycopy(_propertyNames, 0, newPropNames, 0, size);
        _propertyNames = newPropNames;
        Object[] newPropValues = new Object[size + 1];
        System.arraycopy(_propertyValues, 0, newPropValues, 0, size);
        _propertyValues = newPropValues;

        _propertyNames[size] = name;
        _propertyValues[size] = value;
    }

    public Object _getProperty(String name) {
        for (int i = 0; i < _propertyNames.length; ++i) {
            if (_propertyNames[i].equals(name)) {
                return _propertyValues[i];
            }
        }
        if (ENDPOINT_ADDRESS_PROPERTY.equals(name) || USERNAME_PROPERTY.equals(name) || PASSWORD_PROPERTY.equals(name)) {
            return null;
        }
        if (SESSION_MAINTAIN_PROPERTY.equals(name)) {
            return new java.lang.Boolean(false);
        }
        throw new JAXRPCException("Stub does not recognize property: " + name);
    }

    protected void _prepOperation(Operation op) {
        for (int i = 0; i < _propertyNames.length; ++i) {
            op.setProperty(_propertyNames[i], _propertyValues[i].toString());
        }
    }

    //
    //  Begin user methods
    //
    public ArrayOfFood getFoodsWithSimilarNames(java.lang.String partOfName) throws java.rmi.RemoteException {
        // Copy the incoming values into an Object array if needed.
        Object[] inputObject = new Object[1];
        inputObject[0] = partOfName;

        Operation op = Operation.newInstance(_qname_GetFoodsWithSimilarNames, _type_GetFoodsWithSimilarNames, _type_GetFoodsWithSimilarNamesResponse);
        _prepOperation(op);
        op.setProperty(Operation.SOAPACTION_URI_PROPERTY, "http://antiobesity-project.org/GetFoodsWithSimilarNames");
        Object resultObj;
        try {
            resultObj = op.invoke(inputObject);
        } catch (JAXRPCException e) {
            Throwable cause = e.getLinkedCause();
            if (cause instanceof java.rmi.RemoteException) {
                throw (java.rmi.RemoteException) cause;
            }
            throw e;
        }
        ArrayOfFood result;
        // Convert the result into the right Java type.
        // Unwrapped return value
        Object[] getFoodsWithSimilarNamesResultObj = (Object[]) ((Object[]) resultObj)[0];
        if (getFoodsWithSimilarNamesResultObj == null) {
            result = null;
        } else {
            result = new ArrayOfFood();
            Food[] foodArray;
            Object[] foodObj = (Object[]) getFoodsWithSimilarNamesResultObj[0];
            if (foodObj == null) {
                foodArray = null;
            } else {
                int foodArraySize = foodObj.length;
                foodArray = new Food[foodArraySize];
                for (int foodArrayIndex = 0;
                        foodArrayIndex < foodArraySize; ++foodArrayIndex) {
                    if (foodObj[foodArrayIndex] == null) {
                        foodArray[foodArrayIndex] = null;
                    } else {
                        foodArray[foodArrayIndex] = new Food();
                        java.lang.String string;
                        Object IDObj = ((Object[]) foodObj[foodArrayIndex])[0];
                        string = (java.lang.String) IDObj;
                        foodArray[foodArrayIndex].setId(string);
                        java.lang.String string2;
                        Object nameObj = ((Object[]) foodObj[foodArrayIndex])[1];
                        string2 = (java.lang.String) nameObj;
                        foodArray[foodArrayIndex].setName(string2);
                        float a_float;
                        Object caloriesObj = ((Object[]) foodObj[foodArrayIndex])[2];
                        a_float = ((java.lang.Float) caloriesObj).floatValue();
                        foodArray[foodArrayIndex].setCalories(a_float);
                        java.lang.String string3;
                        Object subcategoryIDObj = ((Object[]) foodObj[foodArrayIndex])[3];
                        string3 = (java.lang.String) subcategoryIDObj;
                        foodArray[foodArrayIndex].setSubcategoryId(string3);
                    }
                }
            }
            result.setFoods(foodArray);
        }
        return result;
    }
    //
    //  End user methods
    //
    protected static final QName _qname_Calories = new QName("http://antiobesity-project.org/", "Calories");
    protected static final QName _qname_SubcategoryID = new QName("http://antiobesity-project.org/", "SubcategoryID");
    protected static final QName _qname_Food = new QName("http://antiobesity-project.org/", "Food");
    protected static final QName _qname_GetFoodsWithSimilarNames = new QName("http://antiobesity-project.org/", "GetFoodsWithSimilarNames");
    protected static final QName _qname_GetFoodsWithSimilarNamesResponse = new QName("http://antiobesity-project.org/", "GetFoodsWithSimilarNamesResponse");
    protected static final QName _qname_GetFoodsWithSimilarNamesResult = new QName("http://antiobesity-project.org/", "GetFoodsWithSimilarNamesResult");
    protected static final QName _qname_ID = new QName("http://antiobesity-project.org/", "ID");
    protected static final QName _qname_Name = new QName("http://antiobesity-project.org/", "Name");
    protected static final QName _qname_partOfName = new QName("http://antiobesity-project.org/", "partOfName");
    protected static final Element _type_GetFoodsWithSimilarNames;
    protected static final Element _type_GetFoodsWithSimilarNamesResponse;

    static {
        // Create all of the Type's that this stub uses, once.
        Element _type_partOfName;
        _type_partOfName = new Element(_qname_partOfName, Type.STRING, 0, 1, false);
        ComplexType _complexType_getFoodsWithSimilarNames;
        _complexType_getFoodsWithSimilarNames = new ComplexType();
        _complexType_getFoodsWithSimilarNames.elements = new Element[1];
        _complexType_getFoodsWithSimilarNames.elements[0] = _type_partOfName;
        _type_GetFoodsWithSimilarNames = new Element(_qname_GetFoodsWithSimilarNames, _complexType_getFoodsWithSimilarNames);
        Element _type_ID;
        _type_ID = new Element(_qname_ID, Type.STRING, 0, 1, false);
        Element _type_Name;
        _type_Name = new Element(_qname_Name, Type.STRING, 0, 1, false);
        Element _type_Calories;
        _type_Calories = new Element(_qname_Calories, Type.FLOAT);
        Element _type_CategoryID;
        _type_CategoryID = new Element(_qname_SubcategoryID, Type.STRING);
        ComplexType _complexType_food;
        _complexType_food = new ComplexType();
        _complexType_food.elements = new Element[4];
        _complexType_food.elements[0] = _type_ID;
        _complexType_food.elements[1] = _type_Name;
        _complexType_food.elements[2] = _type_Calories;
        _complexType_food.elements[3] = _type_CategoryID;
        Element _type_Food;
        _type_Food = new Element(_qname_Food, _complexType_food, 0, -1, true);
        ComplexType _complexType_arrayOfFood;
        _complexType_arrayOfFood = new ComplexType();
        _complexType_arrayOfFood.elements = new Element[1];
        _complexType_arrayOfFood.elements[0] = _type_Food;
        Element _type_GetFoodsWithSimilarNamesResult;
        _type_GetFoodsWithSimilarNamesResult = new Element(_qname_GetFoodsWithSimilarNamesResult, _complexType_arrayOfFood, 0, 1, false);
        ComplexType _complexType_getFoodsWithSimilarNamesResponse;
        _complexType_getFoodsWithSimilarNamesResponse = new ComplexType();
        _complexType_getFoodsWithSimilarNamesResponse.elements = new Element[1];
        _complexType_getFoodsWithSimilarNamesResponse.elements[0] = _type_GetFoodsWithSimilarNamesResult;
        _type_GetFoodsWithSimilarNamesResponse = new Element(_qname_GetFoodsWithSimilarNamesResponse, _complexType_getFoodsWithSimilarNamesResponse);
    }
}
